package service;

import dao.UserDAO;
import data.User;
import utils.DAOFactory;

public class UserService
{
	public enum ReturnStatus
	{
		SUCCESS,
		FAILED,
		
		EXIST_USER,
		NOT_EXIST_USER;
	}
	
	private static final UserDAO dao_user = (UserDAO) DAOFactory.getDAOInstance(DAOFactory.DAOClassName.USER);
	
	
	public User getUser(int id){return UserService.dao_user.selectByID(id);}
	public User getUser(String username){return UserService.dao_user.selectByUsername(username);}
	public int getID(String username){return this.getUser(username).getID();}
	
	
	
	public ReturnStatus login(User user)
	{
		return this.login(user.getUsername(), user.getPassword());
	}
	
	
	public ReturnStatus login(String username, String password)
	{
		if (!UserService.dao_user.hasUser(username))
			return UserService.ReturnStatus.NOT_EXIST_USER;
			
		if (UserService.dao_user.checkUser(username, password))
			return UserService.ReturnStatus.SUCCESS;
		return UserService.ReturnStatus.FAILED;
	}
	
	
	public ReturnStatus register(User user)
	{
		return this.register(user.getUsername(), user.getPassword());
	}
	
	
	public ReturnStatus register(String username, String password)
	{
		if (UserService.dao_user.hasUser(username))
			return UserService.ReturnStatus.EXIST_USER;
		UserService.dao_user.insert(username, password);
		return UserService.ReturnStatus.SUCCESS;
	}
	
}







